package com.gx.utils;

import com.alibaba.fastjson.JSON;
import com.gx.response.tree.BootstrapUiTreeNode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Slf4j
public class ResourceToTreeNodeConvert {
    // 获取可选择的Class树形结构
    public static <T> List<BootstrapUiTreeNode> convertClassListToSelectBootstrapUiTreeNodeList(List<T> root,
                                                                                              List<T> nodes) {
        List<BootstrapUiTreeNode> treeNodeList = new ArrayList<BootstrapUiTreeNode>();

        for (T _node : root) {
            BootstrapUiTreeNode treeNode = getExtTree(_node, nodes, true, null);
            treeNodeList.add(treeNode);
        }
        return treeNodeList;
    }

    // 获取不可选择默认全部展开的Class树形结构
    public static <T> List<BootstrapUiTreeNode> convertClassListToBootstrapUiTreeNodeList(List<T> root,
                                                                                        List<T> nodes, List<String> menuIdList) {
        List<BootstrapUiTreeNode> treeNodeList = new ArrayList<BootstrapUiTreeNode>();

        for (T _node : root) {
            BootstrapUiTreeNode treeNode = getExtTree(_node, nodes, false, menuIdList);
            treeNodeList.add(treeNode);
        }
        return treeNodeList;
    }

    public static <T> List<T> getRootNodesList(List<T> list) {
        //获取到所有顶级菜单节点(父节点为0的)
        List<T> rootClassList = new ArrayList<>();
        try {
            for (T o: list) {
                Long pid  = ObjectUtil.get(o,"pid");
                if (pid == 0) {
                    rootClassList.add(o);
                }
            }
        } catch (Exception e) {
            log.error("获取顶级树出现异常：",e);
        }

        return rootClassList;
    }

    public static <T> BootstrapUiTreeNode getExtTree(T node, List<T> list, Boolean selectable, List<String> menuIdList) {
        BootstrapUiTreeNode tree = new BootstrapUiTreeNode();
        if (ObjectUtil.isNotEmpty(node.getClass(), "id")) {
            tree.setId(ObjectUtil.get(node, "id").toString());
        }
        if (ObjectUtil.isNotEmpty(node.getClass(), "pid")) {
            tree.setPid(ObjectUtil.get(node, "pid").toString());
        }
        if (ObjectUtil.isNotEmpty(node.getClass(), "name")) {
            tree.setText(ObjectUtil.get(node, "name").toString());
        } else if (ObjectUtil.isNotEmpty(node.getClass(), "itemName")) {
            tree.setText(ObjectUtil.get(node, "itemName").toString());
        }
        if (ObjectUtil.isNotEmpty(node.getClass(), "icon")) {
            tree.setIcon(ObjectUtil.get(node, "icon").toString());
        }
        log.info("tree:"+ JSON.toJSONString(tree));
        tree.setSelectable(selectable);
        if (!CollectionUtils.isEmpty(menuIdList) && menuIdList.contains(ObjectUtil.get(node, "id").toString())) {
            tree.getState().setChecked(true);
        }

        List<T> childList = getNodes(node, list);

        if (childList.size() > 0) {
            List<BootstrapUiTreeNode> lt = new ArrayList<BootstrapUiTreeNode>();
            for (T m : childList) {
                lt.add(getExtTree(m, list, selectable, menuIdList));
            }
            tree.setNodes(lt);
        }

        tree.setTags(Arrays.asList(childList.size() + ""));

        return tree;
    }


    /**
     * 从 list 整体集合中获取 node 的所有子元素
     *
     * @param node 某个特定节点，返回此节点下的所有子节点
     * @param list 所有节点集合
     * @return
     */
    protected static <T> List<T> getNodes(T node, List<T> list) {
        List<T> resultClassList = new ArrayList<>();
        for (T _zBootstrapUiTreeNodes : list) {
            Long pid = ObjectUtil.get(_zBootstrapUiTreeNodes,"pid");
            if (pid != null && ObjectUtil.get(node,"id") != null && pid.longValue() == ((Long) ObjectUtil.get(node,"id")).longValue()) {
                resultClassList.add(_zBootstrapUiTreeNodes);
            }
        }
        return resultClassList;
    }
}
